#include "DynamicArray.h"
#include <stdlib.h>
#include <stdio.h>

void InitDArray(DArray *da)
{
    da->size = 10;
    da->a = (ElementType*)malloc(sizeof(ElementType)*da->size);//
    da->len = 0;
    if(da->a == NULL)
    {
        printf("malloc error!\n");
        return NULL;
    }
}

void InsertTail(DArray *da, ElementType element)
{
    if(da->len == da->size)//要用等于等于，不用>=
    {
        da->size *= 2;
        ElementType *newP = (ElementType *)malloc(sizeof(ElementType)*da->size);
        if(newP == NULL)
        {
            printf("malloc error!\n");
            return;
        }
        for(int i = 0; i < da->len;i++)
        {
            newP[i] = da->a[i];
          
        }
        free(da->a);
        da->a = newP;

    }
    da->a[da->len++] = element;//
 
}

void PrintDarry(DArray *da)
{
    printf("len = %d size = %d\n", da->len, da->size);
    for(int i = 0; i < da->len; i++)
    {
        printf("%d", da->a[i]);
    }
}
